<?php

namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\db\ActiveRecord;
use app\models\Curd;
use yii\data\Pagination;
header("Content-Type: text/html; charset=utf8");
class CurdController extends Controller {
      public function actionAdd()
      {
      	$post=Yii::$app->request->post();
      	//print_r($post);die;
      	if (!empty($post)) {
	      	$curd = new Curd();
			$curd->name = $post['name'];
			$curd->email = $post['email'];
			$curd->pwd = md5($post['pwd']);
			if ($curd->save()) {
				echo "<script>alert('添加成功');location.href='?r=curd/show'</script>";
				exit;
			}
			else
			{
				echo "<script>alert('添加失败');location.href='?r=curd/add'</script>";
			}
			
      	}
      	return $this->render('add');
      }
      public function actionShow()
      {
      	$query = Curd::find();

		// 得到文章的总数（但是还没有从数据库取数据）
		$count = $query->count();

		// 使用总数来创建一个分页对象
		$pagination = new Pagination(['totalCount' => $count,'pagesize'=>10]);

		// 使用分页对象来填充 limit 子句并取得文章数据
		$articles = $query->offset($pagination->offset)
		    ->limit($pagination->limit)
		    ->all();
         //print_r($data);die;
         return $this->render("show",['data'=>$articles,'pagination' => $pagination]);
      }
      public function actionDels()
      {
      	$get=Yii::$app->request->get('id');
      	//print_r($get);die;
      	$curd = Curd::findOne($get);
      	if ($curd->delete()) {
      		echo "<script>alert('删除成功');location.href='?r=curd/show'</script>";
				exit;
      	}
        else
			{
				echo "<script>alert('删除失败');location.href='?r=curd/show'</script>";
			}
      }
      public function actionUp()
      {
      	
      	$id=Yii::$app->request->get('id');
        $sql="SELECT id,name,email FROM curd WHERE id='$id'";
        $data= Curd::findBySql($sql)->asarray()->one();
       //print_r($data);die;
        return $this->render('up',['data'=>$data]);
      }
      public function actionUpdate()
      {
      	//echo "123";
      	$post=Yii::$app->request->post();
      	//print_r($post);die;
      	if (!empty($post)) {
	      	$curd = Curd::findOne($post['id']);
			$curd->name = $post['name'];
			$curd->email = $post['email'];
			if ($curd->save()) {
				echo "<script>alert('修改成功');location.href='?r=curd/show'</script>";
				exit;
			}
			else
			{
				echo "<script>alert('修改失败');location.href='?r=curd/add'</script>";
			}
			
      	}
      }

}